package com.fbli;
//线程不安全
public class UnsafeBuyTicket {
    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        new Thread(buyTicket,"苦逼的我").start();
        new Thread(buyTicket,"牛逼的你们").start();
        new Thread(buyTicket,"可恶的黄牛").start();
    }
}
class BuyTicket implements Runnable{
    //票
    private int TicketNums=100;
    Boolean flage=true;
    @Override
    public void run() {
        while (flage){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    //加锁变安全
    public synchronized void buy() throws InterruptedException{
        if (TicketNums<=0){
            flage=false;
            return;
        }
        //模拟延时
        Thread.sleep(50);
        //买票
        System.out.println(Thread.currentThread().getName()+"拿到"+TicketNums--);
    }
}